package com.mobile.web.taskAllocationRule;

import com.mobile.service.dto.BusinessDocumentDTO;
import com.mobile.service.dto.TaskAllocationRuleDTO;
import com.mobile.service.dto.TaskAllocationRuleQueryDTO;
import com.mobile.service.dto.WorkOrderDTO;
import com.mobile.service.entity.BusinessDocument;
import com.mobile.service.impl.DocumentGenerationService;
import com.mobile.service.inter.BusinessDocumentService;
import com.mobile.service.inter.TaskAllocationRuleService;
import com.mobile.util.*;
import com.mobile.web.base.BaseController;
import com.mobile.util.config.aop.AutoFill;
import com.mobile.util.config.aop.OperateType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务分配规则表API接口
 */
@RestController
@RequestMapping("/taskAllocationRule")
@Tag(name = "任务分配规则管理接口", description = "包含任务分配规则的查询、新增、修改、删除等操作")
public class TaskAllocationRuleController extends BaseController {

    @Autowired
    private TaskAllocationRuleService taskAllocationRuleService;


    @Autowired
    private DocumentGenerationService documentGenerationService;

    @Autowired
    private BusinessDocumentService businessDocumentService;

    @PostMapping("/addTaskAllocationRule")
    //@RequiresPermissions("taskAllocationRulemana:add:1")
    @Operation(summary = "创建任务分配规则")
    @AutoFill(OperateType.INSERT)
    public Object addTaskAllocationRule(@Valid TaskAllocationRuleDTO dto) {
        Map<String, Object> map;
        try {
            map = taskAllocationRuleService.addTaskAllocationRule(dto);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping(value = "/getTaskAllocationRuleListPage")
    //@RequiresPermissions("taskAllocationRulemana:read:1")
    @Operation(summary = "分页查询任务分配规则")
    public Object getTaskAllocationRuleListPage(@Validated TaskAllocationRuleQueryDTO queryDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            PageResult<TaskAllocationRuleDTO> page = taskAllocationRuleService.getTaskAllocationRuleListPage(queryDTO);
            map.put("page", page);
            map.put("data", page.getPageDatas());
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping(value = "/getTaskAllocationRuleById")
    //@RequiresPermissions("taskAllocationRulemana:edit:1")
    @Operation(summary = "查询任务分配规则详情")
    public Object getTaskAllocationRuleById(
        @Parameter(description = "任务分配规则主键", required = true) 
        @RequestParam("ruleId") Integer id) {
        Map<String, Object> map = new HashMap<>();
        try {
            TaskAllocationRuleDTO dto = taskAllocationRuleService.getTaskAllocationRuleById(id);
            if (StringUtil.isEmpty(dto)) {
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
                map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            } else {
                map.put("data", dto);
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            }
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @PostMapping("/updateTaskAllocationRule")
    //@RequiresPermissions("TaskAllocationRulemana:edit:1")
    @Operation(summary = "编辑任务分配规则")
    @AutoFill(OperateType.UPDATE)
    public Object updateTaskAllocationRule(@Valid TaskAllocationRuleDTO dto) {
        Map<String, Object> map;
        try {
            map = taskAllocationRuleService.updateTaskAllocationRule(dto);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping("/removeTaskAllocationRule")
    //@RequiresPermissions("taskAllocationRulemana:delete:1")
    public Object removeTaskAllocationRule(@RequestParam("ruleId") Integer id) {
        Map<String, Object> map;
        try {
            map = taskAllocationRuleService.removeTaskAllocationRule(id);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping("/getTaskAllocationRuleList")
    //@RequiresPermissions("taskAllocationRulemana:read:1")
    @Operation(summary = "条件查询任务分配规则列表")
    public Object getTaskAllocationRuleList(@Validated TaskAllocationRuleDTO queryDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<TaskAllocationRuleDTO> list = taskAllocationRuleService.getTaskAllocationRuleList(queryDTO);
            map.put("data", list);
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    /**
     * 自动分配任务
     */

    @GetMapping(value = "/autoAssignTask")
    @Operation(summary = "自动分配任务")
    public void autoAssignTask() throws Exception {
        taskAllocationRuleService.autoAssignTask();
    }


    /**
     * 手动分配任务
     */
    @PostMapping(value = "/manualAssignment")
    @Operation(summary = "手动分配任务")
    public Object manualAssignment(@RequestParam Integer workOrderId,
                                   @RequestParam String designatedStaff,
                                   @RequestParam String reason) {

        Map<String, Object> map = new HashMap<>();
        try {
            String userCode = getUserCode();
            map = taskAllocationRuleService.manualAssignment(workOrderId,designatedStaff,userCode,reason);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }
}
